<template>
  <div class="me">
    <!-- 标题 -->
    <van-nav-bar :title="$t('mine.title')" :fixed="true" :border="false" />
    <!-- 登录 -->
    <van-cell-group style="margin-top: 2.84rem;">
      <van-cell
        style="background-color: #3bba63;color:#FFF;"
        label-class="labelClass"
        is-link
        center
      >
        <template slot="title">
          <!-- 已登录状态 start -->
          <div class="personMsg" v-if="token" @click="goToPage('userCenter')">
            <img class="iconImage" :src="userInfo.user_picture" alt />
            <div class="sex" v-if="!userInfo.token">
              <!-- <img :src="userInfo.sex=='1'?'../../assets/image/nantu.png':'../../assets/image/nvtu.png'" alt /> -->
              <img v-show="userInfo.sex=='0'" src="../../assets/image/nvtu.png" alt />
              <img v-show="userInfo.sex=='1'" src="../../assets/image/nantu.png" alt />
            </div>
            <div class="personInfo" v-if="token">
              <span>{{userInfo.username}}</span>
              <span>{{$t('mine.phone')}}：{{userInfo.mobile}}</span>
            </div>
          </div>
          <!-- 已登录状态 end -->
          <!-- 未登录状态 start -->
          <div class="personMsg" v-if="!token">
            <img class="iconImage" src="http://ddbuy.7-orange.cn/img/grey.fb4a97dd.jpg" alt />
            <div class="personInfo">
              <div @click="goToPage('login')">{{$t('mine.login')}}</div>
            </div>
            <!-- 未登录状态 end -->
          </div>
        </template>
      </van-cell>
    </van-cell-group>
    <!-- 订单相关 start -->
    <van-cell-group>
      <van-cell :title="$t('mine.myOrder')" icon="label" :value="$t('mine.allOrder')" :to="{path:'/mine/myOrder', query: {active: -1}}" is-link></van-cell>
      <!--  -->
      <van-grid :border="false">
        <van-grid-item
          v-for="(order,index) in orderData"
          :key="index"
          :text="$t(order.title)"
          :icon="order.icon"
          @click="goTomyOrder(index)"
        />
        <!--  -->
      </van-grid>
    </van-cell-group>
    <!-- 订单相关 end -->

    <!-- 优惠券&地址 start -->
    <van-cell-group style="margin-top:0.4rem">
      <van-cell :title="$t('mine.myBill')" icon="gold-coin" is-link  @click="onClick" />
      <van-cell :title="$t('mine.myLocation')" icon="todo-list" router :to="{path:'/mine/myAddress'}" is-link />
      <!-- -->
    </van-cell-group>
    <!-- 优惠券&地址 end -->

    <!-- 我的绿卡 start -->
    <van-cell-group style="margin-top:0.4rem"  @click="onClick">
      <van-cell is-link icon="vip-card">
        <template slot="title">
          <span class="custom-title">{{$t('mine.myCar')}}</span>
          <van-tag type="danger" :round="true">NEW</van-tag>
        </template>
      </van-cell>
    </van-cell-group>
    <!-- 我的绿卡 end -->

    <!-- 其他 -->
    <van-cell-group style="margin-top:0.4rem">
      <!-- 联系客服 -->
      <van-cell :title="$t('mine.servier')" icon="phone" :value="$t('mine.servierTime')" @click="onClick" />
      <!-- 意见反馈 -->
      <van-cell :title="$t('mine.feedback')" icon="comment-circle" is-link @click="onClick" />
      <!-- 语言切换 -->
      <van-cell :title="$t('mine.switchLanguage')" icon="clock" is-link :to="{path:'/switchLanguage'}" />
    </van-cell-group>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 订单
      orderData: [
        { id: 0, title: 'mine.itemsTitle[1]', icon: 'cart-circle-o' },
        { id: 1, title: 'mine.itemsTitle[2]', icon: 'gift-o' },
        { id: 2, title: 'mine.itemsTitle[3]', icon: 'smile-comment-o' },
        { id: 3, title: 'mine.waitingFeedback', icon: 'cash-back-record' }
      ],
      token: localStorage.getItem('token'),
      userInfo: []
    }
  },
  methods: {
    onClick () {
      this.$toast('暂无')
    },
    // 跳转到我的订单
    goTomyOrder (index) {
      return this.$router.push({ path: '/mine/myOrder', query: { active: index } })
    },
    goToPage (name) {
      if (name === 'login') {
        return this.$router.push(`/${name}`)
      } else if (name === 'userCenter') {
        return this.$router.push(`/mine/${name}`)
      }
    }
  },
  created () {
    if (localStorage.getItem('token')) {
      let id = localStorage.getItem('userId')
      this.$http.get(`/setting/${id}`).then(res => {
        this.userInfo = res.data.data[0]
      })
    }
  }
}
</script>

<style lang="less">
.me {
  width: 100%;
  background-color: #f5f5f5;
  margin-bottom: 3rem;

  .version {
    margin: 0 auto;
    text-align: center;
    font-size: 0.6rem;
    background-color: #ffffff;
    height: 2rem;
    color: grey;
    line-height: 2rem;
  }

  .van-nav-bar {
    background-color: #3bba63;
    font-size: 0.6rem;
  }

  .van-nav-bar__title {
    color: white;
  }

  .personMsg {
    display: flex;
    align-items: center;

    .sex {
      position: absolute;
      top: 4.3rem;
      left: 4.3rem;
      width: 1.1rem;
      height: 1.1rem;
      img {
        width: 1rem;
        height: 1rem;
      }
    }
    img {
      width: 4.8125rem;
      height: 4.8125rem;
      border-radius: 50%;
    }

    .personInfo {
      display: flex;
      flex-direction: column;
      margin-left: 0.8rem;
    }
  }

  .van-cell__left-icon {
    color: #45c763;
    font-size: 1.2rem;
  }
}
</style>
